# -*- coding: utf-8 -*-
# @File:     generate.py
# @Author:
# @DateTime: 2025/06/04/14:51
from app.db.base_class import Base, gen_uuid
from sqlalchemy import Boolean, Column, Integer, String, VARCHAR, BIGINT, SmallInteger, DateTime, ForeignKey, FLOAT, TEXT
from datetime import datetime
from sqlalchemy_serializer import SerializerMixin
from sqlalchemy.orm import relationship
from app.models.system import Users


class MenuList(Base, SerializerMixin):
    """
    功能菜单
    """
    __tablename__ = "menu_list"
    name = Column(VARCHAR(512), comment="名字")
    type = Column(Integer, comment="类型", nullable=True)
    image = Column(VARCHAR(512), nullable=True, comment="图片")
    label = Column(VARCHAR(256), nullable=True, comment="标签")
    describe = Column(VARCHAR(512), nullable=True, comment="描述")
    sort = Column(BIGINT, default=1, comment="排序", server_default="1")
    point = Column(FLOAT, default=1, comment="点数", server_default="1")
    queue = Column(VARCHAR(512), nullable=True, comment="队列")
    order = Column(VARCHAR(512), nullable=True, comment="排序")
    input = Column(TEXT, nullable=True, comment="输入")
    workflowId = Column(VARCHAR(512), nullable=True, comment="工作流")
    enable = Column(Integer, default=1, comment="可用", server_default="1")


class UserMembership(Base, SerializerMixin):
    """
    用户套餐
    """
    __tablename__ = "user_membership"
    total = Column(BIGINT, default=0, comment="总数", server_default="0")
    use_number = Column(BIGINT, default=0, comment="使用数量", server_default="0")
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='CASCADE'))
    user = relationship('Users', backref='membership')  # , lazy='dynamic'
    task_num = Column(Integer, default=1, comment="任务数", server_default="1")
    priority = Column(SmallInteger, default=3, comment="优先级", server_default="3")
    start_time = Column(DateTime, nullable=True, comment="开始时间")
    expire_time = Column(DateTime, nullable=True, comment="过期时间")
    grade = Column(Integer, default=1, comment="级别", server_default="1")


class GenerateTask(Base, SerializerMixin):
    """
    生成任务
    """
    __tablename__ = "generate_task"

    task_uuid = Column(VARCHAR(128), default=gen_uuid, index=True, unique=True, comment="任务id")
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='CASCADE'))
    user = relationship('Users', backref='task')  # , lazy='dynamic'
    menu_id = Column(Integer, ForeignKey('menu_list.id', ondelete='SET NULL'), nullable=True)
    menu = relationship('MenuList', backref='task')
    state = Column(Integer, default=1, comment="状态", server_default="1")  # 1.排队 2.生成中 3.已完成 4.取消 -1.失败
    params = Column(TEXT, nullable=True, comment="提交的参数")

    send_queue = Column(VARCHAR(512), nullable=True, comment="发送队列")
    order_queue = Column(VARCHAR(512), nullable=True, comment="排序队列")
    error_describe = Column(TEXT, nullable=True, comment="错误描述")


class GenerateRecord(Base, SerializerMixin):
    """
    生成记录
    """
    __tablename__ = "generate_record"

    g_task_id = Column(Integer, ForeignKey('generate_task.id', ondelete='SET NULL'), nullable=True)
    g_task = relationship('GenerateTask', backref='record')  # , lazy='dynamic'
    task_uuid = Column(VARCHAR(128), default=gen_uuid, index=True, unique=True, comment="任务id")
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='CASCADE'))
    user = relationship('Users', backref='record')  # , lazy='dynamic'
    menu_id = Column(Integer, ForeignKey('menu_list.id', ondelete='SET NULL'), nullable=True)
    menu = relationship('MenuList', backref='record')
    state = Column(Integer, default=1, comment="状态", server_default="1")
    params = Column(TEXT, nullable=True, comment="提交的参数")
    send_queue = Column(VARCHAR(512), nullable=True, comment="发送队列")
    order_queue = Column(VARCHAR(512), nullable=True, comment="排序队列")
    error_describe = Column(TEXT, nullable=True, comment="错误描述")


class GenerateParamImage(Base, SerializerMixin):
    """
    生成参数图
    """
    __tablename__ = "generate_param_image"

    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='SET NULL'), nullable=True)
    user = relationship('Users', backref='param_image')  # , lazy='dynamic'
    task_id = Column(Integer, ForeignKey('generate_task.id', ondelete='SET NULL'), nullable=True)
    task = relationship('GenerateTask', backref='param_image')  # , lazy='dynamic'
    record_id = Column(Integer, ForeignKey('generate_record.id', ondelete='SET NULL'), nullable=True)
    record = relationship('GenerateRecord', backref='param_image')  # , lazy='dynamic'
    path = Column(VARCHAR(512), comment="图片")
    file_type = Column(VARCHAR(128), comment="文件类型", default='png', server_default='png')


class GenerateResult(Base, SerializerMixin):
    """
    生成结果
    """
    __tablename__ = "generate_result"

    task_id = Column(Integer, ForeignKey('generate_task.id', ondelete='SET NULL'), nullable=True)
    task = relationship('GenerateTask', backref='result')  # , lazy='dynamic'
    record_id = Column(Integer, ForeignKey('generate_record.id', ondelete='SET NULL'), nullable=True)
    record = relationship('GenerateRecord', backref='result')  # , lazy='dynamic'
    path = Column(VARCHAR(1024), comment="结果路径")
    file_type = Column(VARCHAR(128), comment="文件类型", default='png', server_default='png')
    download = Column(Integer, default=0, comment="下载", server_default="0")
    is_share = Column(Integer, default=0, comment="分享", server_default="0")


class GenerateShare(Base, SerializerMixin):
    """
    生成分享
    """
    __tablename__ = "generate_share"

    path = Column(VARCHAR(1024), comment="结果路径")
    file_type = Column(VARCHAR(128), comment="文件类型", default='png', server_default='png')
    menu_id = Column(Integer, ForeignKey('menu_list.id', ondelete='SET NULL'), nullable=True)
    menu = relationship('MenuList', backref='share')
    params = Column(TEXT, nullable=True, comment="提交的参数")
    examine = Column(Integer, default=1, comment="审核", server_default="1")  # 1.等待 2.同意 -1.拒绝
    user_id = Column(Integer, ForeignKey('system_users.id', ondelete='SET NULL'), nullable=True)
    user = relationship('Users', backref='share')  # , lazy='dynamic'
    hot = Column(Integer, default=0, comment="热度", server_default="0")


